package cn.edu.hunau.config;
import cn.edu.hunau.common.BusinessException;
import cn.edu.hunau.common.ResultCodeEnum;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.boot.web.servlet.error.ErrorAttributes;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.NoHandlerFoundException;
import org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver;



//@Configuration
public class CustomNoHandlerFoundConfig {

    @Bean
    public AbstractHandlerExceptionResolver noHandlerFoundResolver(ErrorAttributes errorAttributes) {
        return new AbstractHandlerExceptionResolver() {
            @Override
            protected ModelAndView doResolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
                System.out.println("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
                ex.printStackTrace();
                if (ex instanceof NoHandlerFoundException) {
                    // 自定义处理逻辑，例如返回自定义的错误消息或状态码
                    throw new BusinessException(ResultCodeEnum.NO_FOUND_EXCEPTION);
                }
                return null; // 如果不是 NoHandlerFoundException，则不处理
            }
        };
    }
}
